package ex.mlist;




import android.app.Activity;
import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class MlistActivity extends ListActivity {
	ListView musiclist;
	Cursor musiccursor;
	int music_column_index;
	int count;
	MediaPlayer mMediaPlayer;
	LayoutInflater minflater;
	

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		musiclist = getListView();
		init_phone_music();
	}
	
	
	
	
	
	
	private void init_phone_music() {
		System.gc();
		String[] proj = { MediaStore.Audio.Media._ID,
				MediaStore.Audio.Media.DATA,
				MediaStore.Audio.Media.DISPLAY_NAME,
				MediaStore.Video.Media.SIZE };
		musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null, null);
		count = musiccursor.getCount();
		musiclist.setAdapter(new MusicAdapter(getApplicationContext()));
		//musiclist.setOnClickListener(musiclistener);
		mMediaPlayer = new MediaPlayer();
		
		
	}
	
	
	public void addButtonHandler(View v){
		//Toast.makeText(getApplicationContext(), "dsfcds", Toast.LENGTH_LONG).show();
		
		
		
		int firstPosition=getListView().getFirstVisiblePosition();
		int listcount;
		SharedPreferences tones = getSharedPreferences("MYLIST", 0);
		SharedPreferences.Editor editor = tones.edit();
		CheckBox cbx;
		//View view;
		
		int total;
		total=getListView().getLastVisiblePosition();
		System.out.println(firstPosition+" "+total);
		try
		{
		listcount=tones.getInt("count", 0);
		for(int i=firstPosition;i<total;i++)
		{
			v=getListView().getChildAt(i);
			TextView tv=(TextView) v.findViewById(R.id.textView1);
			cbx=(CheckBox) v.findViewById(R.id.checkBox1);
		     System.out.println(i+"&&&&"+tv.getText().toString());
			if(cbx.isChecked())
			{
				
				 
				System.out.println(tv.getText().toString()+" "+listcount);
				
				editor.putString(listcount+"",tv.getText().toString()) ;
				listcount+=1;
				System.out.println(listcount);
				editor.putInt("count", listcount);
				editor.commit();
				System.out.println("count"+tones.getInt("count", 0));
				
		    }
			
			
		}
		Intent intent=new Intent(MlistActivity.this,ManagerActivity1.class);
		startActivity(intent);
		
		}
		catch(Exception e){
		e.printStackTrace();
			
		}
		
		
		
	}

	
	private void playList(String name, String path,int size) 
	{

		

		try {

			if (mMediaPlayer.isPlaying()) {
				mMediaPlayer.reset();
			}

			mMediaPlayer.setDataSource(path);
			mMediaPlayer.prepare();
			mMediaPlayer.start();
		}

		catch (Exception e) {

		}
	}
	
	
	
	 protected void onStop()
	 {
		 
		 mMediaPlayer.stop();
		 super.onStop();
	 }
	 
	 
	public class MusicAdapter extends BaseAdapter {
		private Context mContext;

		public MusicAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return count;
		}

		public Object getItem(int position) {
			return musiccursor.moveToPosition(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final int pos=position;
			System.gc();
			minflater=LayoutInflater.from(mContext.getApplicationContext());
			ViewHolder holder;
	
			String id = null;
			int filename=0;
			if (convertView == null) 
			{
                 holder=new ViewHolder();
                 convertView= minflater.inflate(R.layout.musiclayout, null);
                 holder.tv=(TextView) convertView.findViewById(R.id.textView1);
                 holder.cb=(CheckBox) convertView.findViewById(R.id.checkBox1);
                 music_column_index = musiccursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME);
			        musiccursor.moveToPosition(pos);
				 id = musiccursor.getString(music_column_index);
				 filename= musiccursor.getColumnIndex(MediaStore.Audio.Media.DATA);
				 final String name=id;
				 
				 final String path=musiccursor.getString(filename);
				 final int size=musiccursor.getColumnIndex(MediaStore.Audio.Media.SIZE);
                 convertView.setClickable(true);
                 convertView.setFocusable(false);
                 convertView.setOnClickListener(new OnClickListener() {
                	
					
					public void onClick(View v) {
						
						playList(name,path,size);
					}

					
				});
				convertView.setTag(holder);
				
			} else
				holder = (ViewHolder) convertView.getTag();
			
			music_column_index = musiccursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME);
	        musiccursor.moveToPosition(pos);
			id = musiccursor.getString(music_column_index);
			holder.tv.setText(id);
			return convertView;
		}
		
		
	}
	static class ViewHolder
	{
	   
		String id;
		TextView tv;
		CheckBox cb;
	
	}
}